﻿using ra2map_plus.config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ra2map_plus.data;
using ra2map_plus.service;
using System.IO;

namespace ra2map_plus
{
    public partial class Form1 : Form
    {
        private readonly ApplicationConfig ApplicationConfig;
        private Ra2MapPlusService service;

        public Form1()
        {
            InitializeComponent();

            this.ApplicationConfig = new ApplicationConfig();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void button1_DragDrop(object sender, DragEventArgs e)
        {
            Console.WriteLine(sender);
            var data = e.Data;
            string[] filePaths = (string[])data.GetData(DataFormats.FileDrop);
            if (filePaths.Length > 0)
            {
                string p = filePaths[0];
                if (Directory.Exists(p))
                {
                    openProject(p);
                } else if (File.Exists(p))
                {
                    FileInfo fi = new FileInfo(p);
                    string d = fi.Directory.FullName;
                    openProject(d);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowserDialog1.ShowDialog();
            Console.WriteLine(dialogResult);
            string selectedPath = folderBrowserDialog1.SelectedPath;
            if (DialogResult.OK.Equals(dialogResult) && !string.IsNullOrWhiteSpace(selectedPath))
            {
                openProject(selectedPath);
            }
        }

        private void openProject(string projectLocation)
        {
            this.service = new Ra2MapPlusService(projectLocation);
            // 打开新窗口 并传递service参数
            Console.WriteLine(this.service);
            Form2 form2 = new Form2(this, service);
            this.Hide();
            form2.Show();
        }
    }
}
